package com.narunas;

import com.narunas.animation.ContainerAnimationBuilder;
import com.narunas.containers.ContainerBuilder;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.view.animation.Animation.AnimationListener;
import android.widget.AbsoluteLayout;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.widget.AbsoluteLayout.LayoutParams;

public class Dialer extends Activity {
	
	private ViewGroup container;
	private LinearLayout dPanel;
	private Button openCloseBtn;
	
	private Boolean isOpen;
	
	public Dialer() {
		// TODO Auto-generated constructor stub
	}
	@Override
	 public void onCreate(Bundle savedInstanceState) {
	        super.onCreate(savedInstanceState);
	        
	        container = ContainerBuilder.createDialerContainer(this);
	        setContentView(container);
	        
	       
	       
	        //showToast("The button - " + container.getChildCount());
	       dPanel = (LinearLayout)container.getChildAt(0);
	       openCloseBtn = (Button)dPanel.getChildAt(0);
	       isOpen = false;
	       
	       Log.d("EXP-VIEW", "the button : " + openCloseBtn.toString());
	       openCloseBtn.setOnClickListener(onHandleOpenClose);
	       

	       container.buildDrawingCache();
	       
	      // container.setAnimationCacheEnabled(true);
	       
	     //  container.layout(200, 0, 0, 0);
	       
	      setDialerPanelAnimation();
	       
	       
	
	};
	
	
	OnClickListener onHandleOpenClose = new OnClickListener() {
		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			Log.d("EXT-Dialer", "The Open Close Button was clicked");
			 
			
			setDialerPanelAnimation();

		}
	
	};
	 	
	protected final void setDialerPanelAnimation() {
		
		
	   // container.setDrawingCacheEnabled(true);
		LayoutAnimationController controller;
		
		if(!isOpen){
			//controller = new LayoutAnimationController(ContainerAnimationBuilder.createTranslateLayoutAnimation(isOpen), 0.95f);
			//controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.in_from_left));
			//dPanel.setAnimation(AnimationUtils.loadAnimation(this, R.anim.in_from_left));
		} else {
			
			//controller = new LayoutAnimationController(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
			//dPanel.setAnimation(AnimationUtils.loadAnimation(this, R.anim.out_to_left));
		}
		      //new LayoutAnimationController(ContainerAnimationBuilder.createTranslateLayoutAnimation(isOpen), 0.95f);
		
		controller = new LayoutAnimationController(ContainerAnimationBuilder.createAnimationSet(isOpen, onAnimation), 0.95f);
		
		//controller.
		container.setLayoutAnimation(controller);
		
		
		//dPanel.setLayoutAnimation(controller);
		isOpen = !isOpen;
		//container.forceLayout();
	}
	
	public void handleEndAnimation(Animation anim) {
		
		//Log.d("EXP-Animaton", "animation ended: X : " );
		dPanel.getAnimation().setFillAfter(true);
		Log.d("EXP-Animaton", "animation ended: X : "  + dPanel.getLeft());
		//LayoutParams params = new LayoutParams(dPanel.getLeft(), 0, 0, 0)
		//params.y = 0;
		
		//dPanel.setLayoutParams(new LayoutParams(dPanel.getLeft(), 0, 0, 0));
		
		
		
		
	}
	public void handleStartAnimation(Animation anim ) {
		
		Log.d("EXP-Animation", "animation started");
		
	}
	AnimationListener onAnimation = new AnimationListener() {

		@Override
		public void onAnimationEnd(Animation animation) {
			// TODO Auto-generated method stub
			handleEndAnimation(animation);
		}

		@Override
		public void onAnimationRepeat(Animation animation) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onAnimationStart(Animation animation) {
			// TODO Auto-generated method stub
			handleStartAnimation(animation);
			
		}
		
		
		
	};
	
	private final void handleOpenCloseAction() {
		
		Toast.makeText(this, "Drag button clicked", Toast.LENGTH_SHORT);
		
		
	}
	
	protected final void showToast(String st) {
		
		
		Toast.makeText(this, st, Toast.LENGTH_SHORT);
		
	}
}
